<?php

namespace App\Http\Controllers\YApi;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;

use App\Models\UsersReg;

class UserLoginController extends Controller
{

    public function userlogin()
    {
        //用户登录        
        $username = request::input('username');
        $password = request::input('password'); 

        $resdata = UsersReg::where('user_name', $username)->first();
        if($resdata == null)
        {
            // 用户名不正确,返回 ｛"code": 0｝
            return array(
              'code' => -1, 
              'msg'=>'用户名不存在'
            );
        }

        // 加密后的密码
        $password = $this->compile_password($password, $resdata->salt); 
        if($password != $resdata->password)
        {
            // 密码不正确，返回 {"code": 1}
            return array(
                'code' => -2,
                'msg' => '密码不正确'
            );
        }

        return $resdata;
    }


    /**
     * 根据 salt 混淆密码
     *
     * @param  string
     * @param  string
     * @return string
     */
    public function compile_password($password, $salt)
    {
        $password = md5(md5($password) . $salt);
        return $password;
    }
    
}